<?php
/**
 * Created by PhpStorm.
 * User: ulinix
 * Date: 2017/10/8
 * Time: 10:43
 */
namespace Admin\Controller;
class LinkClassifyController extends CheckController{
    public function index(){
        $this->page_title="سەھىپىلەر";
        $count=M("linkcate")->count();
        $classifys=_sort(M("linkcate")->order("sort desc")->select());
        $this->assign('classifys',$classifys);
        $this->display("index");
    }
    public function add(){
        $this->page_title="سەھىپە قوشۇش";
        $classify=_sort(M("linkcate")->select());
        $this->assign('classify',$classify);
        $this->display("add");
    }
    //接收数据并且写入到数据库
    public function addhend(){
        if(stripos(trim(I('post.name')),"،")){
            $post_data['names']=explode("،",I('post.name'));
        }elseif (stripos(trim(I('post.name')),",")){
            $post_data['names']=explode(",",I('post.name'));
        }elseif (stripos(trim(I('post.name')),"  ")){
            $post_data['names']=explode("  ",I('post.name'));
        }elseif(stripos(trim(I('post.name'))," ")){
            $post_data['names']=I('post.name');
        }elseif(is_numeric(trim(I('post.name')))){
            $post_data['names']=I('post.name');
        }
        $post_data['sort']=I('post.sort');
        $post_data['pid']=I('post.pid');
        $post_data['status']=I('post.status');
        $rules = array(
            array('name','require','سەھىپە نامىنى چۇقۇم يېزىڭ！',1), //默认情况下用正则进行验证
            array('pid','require','ماس سەھىپە تاللاڭ',2)
        );
        if(!M("linkcate")->validate($rules)->create()){
            $this->error(M("linkcate")->getError());
        }else{
            $i=M("linkcate")->max('sort');
            if(is_array($post_data['names'])){
                foreach ($post_data['names'] as $name){
                    $name=htmlspecialchars(trim($name));
                    $i++;
                    $data=array(
                        'name'=>$name,
                        'sort'=>I('post.sort')+$i,
                        'pid'=>I('post.pid'),
                        'status'=>I('post.status')
                    );
                    $result=M("linkcate")->add($data);
                }
            }else{
                $result=M("linkcate")->add(I('post.'));
            }
            $ajax=1;
            if($ajax==1){
                $data=array("status"=>1,"message"=>"مۇۋاپىقىيەتلىك قوشۇلدى !");
            }
            $this->ajaxReturn($data);

        }
    }
    //编辑分类
    public function edit(){
        $id=I('get.id');
        //获取要编辑的一条数据
        $classifys=M("linkcate")->find($id);
        $this->assign("classifys",$classifys);
        $this->display("edit");
    }
    //接收数据并且保存到数据库
    public function savehend(){
        $rules = array(
            array('name','require','سەھىپە نامىنى چۇقۇم يېزىڭ！',1), //默认情况下用正则进行验证
//            array('pid','require','ماس سەھىپە تاللاڭ',2)
        );
        $User=M("linkcate");
        if(!$User->validate($rules)->create()){
            $this->error($User->geterror(),"",1);
        }else{
            $result =$User->save(); // 保存到数据库
            if($result!==false){
                $this->display("../index");
                _check_alert("ئۇچۇر ئۆزگەرتىش مۇۋاپىقيەتلىك بولدى !","index.html",1);
            }
        }
    }
//用ajax删除分类
    public function del(){
        $id=I('post.id');
        //获取要编辑的一条数据
        $data=M("linkcate")->delete($id);
        if(!$data){
            $data=array("status"=>0,"message"=>"ئۆچۈرۈش مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"message"=>"ئۆچۈرۈش مۇۋاپىقيەتلىك بولدى !");
        };
        $this->ajaxReturn($data);
    }
    //用ajax分类状态
    public function sortStatus(){
        $id=I('post.id');
        $post['status']=I('post.status');
        if(!M('linkcate')->where("id='$id'")->save($post)){
            $data=array("status"=>0,"content"=>"مەغلۇب بولدى ، قايتا سىناڭ !");
        }else{
            $data=array("status"=>1,"content"=>"مۇۋاپقىيەتلىك بولدى !");
        }
        $this->ajaxReturn($data);
    }
}